/**   
 *
 * @author ilya portnyagin iportnyagin@gmail.com
 */

package ru.portnyagin.helpdeskru.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;
import ru.portnyagin.helpdeskru.model.StateRequest;
import ru.portnyagin.helpdeskru.util.JsfUtil;

@ManagedBean(name = "stateRequestBean")
@ApplicationScoped
public class StateRequestBean implements Serializable {

    @EJB
    private ru.portnyagin.helpdeskru.service.StateRequestService stateRequestService;
    
    private Map<String, StateRequest> mapStateRequest;  // ID -> StateRequest
    private Map<String, List<StateRequest>> mapAvailableStateRequest; // ID -> List<StateRequest>

    private List<StateRequest> listStateRequest;
    
    public StateRequestBean() {
    }
    
    @PostConstruct
    private void post() {
        mapStateRequest = new HashMap();
        stateRequestService.findMapStateRequest(mapStateRequest);
        
        initMapAvailableStateRequest();
    }    
    
    public SelectItem[] getSelectItemsStateRequest() {
        if(listStateRequest == null) {
            listStateRequest = new ArrayList<>();
            listStateRequest = stateRequestService.findAll();
        }
        return JsfUtil.getSelectItems(listStateRequest, true);
    }
    
    private void initMapAvailableStateRequest() {
        mapAvailableStateRequest = new HashMap();
        

        for (String curStateRequest: mapStateRequest.keySet()) {
            
            List<StateRequest> listStateReques = new ArrayList<>();
        
            listStateReques.add(mapStateRequest.get(curStateRequest));
            
            if(curStateRequest.equals("OPENED") || curStateRequest.equals("REOPENED")) {
                listStateReques.add(mapStateRequest.get("INPROGRESS"));
            }

            if(curStateRequest.equals("INPROGRESS")) {
                listStateReques.add(mapStateRequest.get("DONE"));
                listStateReques.add(mapStateRequest.get("NEEDINFO"));
                listStateReques.add(mapStateRequest.get("HOLDED"));
            }

            if(curStateRequest.equals("HOLDED")) {
                listStateReques.add(mapStateRequest.get("INPROGRESS"));
            }

            if(curStateRequest.equals("CLOSED")) {
            }

            if(curStateRequest.equals("NEEDINFO")) {
                listStateReques.add(mapStateRequest.get("REOPENED"));
                listStateReques.add(mapStateRequest.get("CLOSED"));
            }

            if(curStateRequest.equals("DONE")) {
                listStateReques.add(mapStateRequest.get("REOPENED"));
                listStateReques.add(mapStateRequest.get("CLOSED"));
            }
            
            mapAvailableStateRequest.put(curStateRequest, listStateReques);
        }
    }


    public List<StateRequest> getListAvailableStateRequest(String state) {
        return mapAvailableStateRequest.get(state);
    }

    public Map<String, StateRequest> getMapStateRequest() {
        return mapStateRequest;
    }
            
    
}
